package com.crm.controller.client;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crm.pojo.Back;
import com.crm.pojo.ClientInfo;
import com.crm.pojo.ClientNeed;
import com.crm.pojo.Contact;
import com.crm.pojo.Dispatcher;
import com.crm.pojo.DispatcherDetail;
import com.crm.pojo.PlaningBack;
import com.crm.pojo.Purchase;
import com.crm.pojo.Purchaseproductiondetail;
import com.crm.pojo.SaleChance;
import com.crm.pojo.Scheme;
import com.crm.pojo.Userinfo;
import com.crm.service.before_sale.CompetitorService;
import com.crm.service.client.ClientInfoService;
import com.crm.service.purchaseService.PurchaseService;
import com.crm.service.purchaseService.PurproductDetailsService;
import com.crm.service.saling.CODetailsService;
import com.crm.service.saling.ContractService;
import com.crm.vo.CODetailsVO;
import com.crm.vo.ClientDetailVo;
import com.crm.vo.OfferDetailsVO;
import com.crm.vo.PlanDetailsVO;

@Controller
public class ClientDetailController {

	@Autowired
	private ClientInfoService clientInfoService;
	@Autowired
	private CODetailsService cODetailsService;
	@Autowired
	private PurchaseService purchaseService;
	@Autowired
	private PurproductDetailsService purproductDetailsService;
	@Autowired
	private CompetitorService competitorService;

	/**
	 * 客户视图
	 * 
	 * @param model
	 * @param cid
	 * @return
	 */
	@GetMapping("/to-client-detail")
	public String toClientDetail(Model model, Long cid) {
		// 客户信息
		ClientInfo clientInfo = clientInfoService.load(cid);
		model.addAttribute("clientInfo", clientInfo);
		// 联系人
		List<Contact> contacts = clientInfoService.queryContactByCid(cid);
		model.addAttribute("contacts", contacts);

		// 客户售中数据
		List<ClientDetailVo> saleclientDetailVos = new ArrayList<>();
		//销售机会
		List<SaleChance> saleChances = competitorService.getSaleChanceListByClientId(cid);
		for (SaleChance saleChance : saleChances) {
			ClientDetailVo clientDetailVo = new ClientDetailVo(
					saleChance.getChId(), 
					"销售机会", 
					saleChance.getChanceTheme(), 
					clientInfoService.userName(saleChance.getUserId().longValue()), 
					saleChance.getUpdateDate(), 
					saleChance.getExpectMoney(), 
					saleChance.getState());
			saleclientDetailVos.add(clientDetailVo);
		}
		model.addAttribute("saleChancesLength", saleChances.size());
		
		//客户需求
		List<ClientNeed> clientNeeds = competitorService.getClientNeedListByClientId(cid);
		for (ClientNeed clientNeed : clientNeeds) {
			ClientDetailVo clientDetailVo = new ClientDetailVo(
					clientNeed.getNeedId(), 
					"详细需求", 
					clientNeed.getNeedTheme(), 
					"", 
					clientNeed.getRecordTime(), 
					null, 
					"");
			saleclientDetailVos.add(clientDetailVo);
		}
		model.addAttribute("clientNeedsLength", clientNeeds.size());
		
		//解决方案
		List<Scheme> schemes = competitorService.getSchemeListByClientId(cid);
		for (Scheme scheme : schemes) {
			ClientDetailVo clientDetailVo = new ClientDetailVo(
					scheme.getScId(), 
					"解决方案", 
					scheme.getScTheme(), 
					clientInfoService.userName(scheme.getChragePeopleId().longValue()), 
					scheme.getSubmintTime(),
					null, 
					scheme.getScCheck());
			saleclientDetailVos.add(clientDetailVo);
		}
		model.addAttribute("schemesLength", schemes.size());
		
		//报价
		List<OfferDetailsVO> offerDetailsVOs = competitorService.getOfferDetailVOListByClientId(cid);
		for (OfferDetailsVO offerDetailsVO : offerDetailsVOs) {
			ClientDetailVo clientDetailVo = new ClientDetailVo(
					offerDetailsVO.getOfferDetail().getDeId(), 
					"报价", 
					offerDetailsVO.getOfferDetail().getOffer().getTheme(), 
					clientInfoService.userName(offerDetailsVO.getOfferDetail().getOffer().getUserId().longValue()),
					offerDetailsVO.getOfferDetail().getOffer().getDate(), 
					offerDetailsVO.getOfferDetail().getTotalMoney(), 
					offerDetailsVO.getOfferDetail().getOffer().getCheckState());
			saleclientDetailVos.add(clientDetailVo);
		}
		model.addAttribute("offerDetailsVOsLength", offerDetailsVOs.size());
		

		// 该客户得所有订单，以及每个订单对应的订单详情
		List<CODetailsVO> coDetailsVOs = cODetailsService.getAllCODetailVOByClientId(cid);
		Double heyueMoney = 0.0;
		for (CODetailsVO coDetailsVO : coDetailsVOs) {
			ClientDetailVo clientDetailVo = new ClientDetailVo(coDetailsVO.getCoDetail().getContract().getContractId(),
					"合同", coDetailsVO.getCoDetail().getContract().getContractTopic(),
					clientInfoService.userName(clientInfo.getUserId().longValue()),
					coDetailsVO.getCoDetail().getCOTime(), coDetailsVO.getCoDetail().getContract().getContractMoney(),
					coDetailsVO.getCoDetail().getCOState());
			saleclientDetailVos.add(clientDetailVo);
			heyueMoney += coDetailsVO.getCoDetail().getContract().getContractMoney().doubleValue();
		}
		model.addAttribute("heyueMoney", heyueMoney);
		model.addAttribute("hetongLength", coDetailsVOs.size());

		Double fahuoMoney = 0.0;
		// 客户的对应的发货信息
		List<DispatcherDetail> dispatcherDetails = cODetailsService.getDispatcherDetailRecordListByClientId(cid);
		for (DispatcherDetail dispatcherDetail : dispatcherDetails) {
			ClientDetailVo clientDetailVo = new ClientDetailVo(dispatcherDetail.getDispatcherDetailsId(), "发货单", " ",
					clientInfoService.userName(clientInfo.getUserId().longValue()),
					dispatcherDetail.getDispatcher().getFkDispatcherCustomer(), dispatcherDetail.getProductAccount(),
					dispatcherDetail.getDispatcher().getDispatcherState());
			saleclientDetailVos.add(clientDetailVo);
			fahuoMoney += dispatcherDetail.getProductAccount().doubleValue();
		}
		model.addAttribute("fahuoMoney", fahuoMoney);
		model.addAttribute("fahuoLength", dispatcherDetails.size());

		// 客户的对应的发货单
		/*
		 * List<Dispatcher> dispatchers =
		 * cODetailsService.getDispatcherRecordListByClientId(cid); for (Dispatcher
		 * dispatcher : dispatchers) { List<DispatcherDetail> dispatcherDetails =
		 * dispatcher.getDispatcherDetails(); for (DispatcherDetail dispatcherDetail :
		 * dispatcherDetails) { ClientDetailVo clientDetailVo = new ClientDetailVo(
		 * dispatcher.getDispatcherId(), "发货单", " ", clientInfo.getCname(), null,
		 * dispatcherDetail.getProductAccount()); clientDetailVos.add(clientDetailVo); }
		 * }
		 */

		Double jihuahuikuanMoney = 0.0;
		// 客户的对应回款计划
		List<PlaningBack> planingBacks = cODetailsService.getPlaningBackRecordListByClientId(cid);
		for (PlaningBack planingBack : planingBacks) {
			ClientDetailVo clientDetailVo = new ClientDetailVo(planingBack.getPlaningBackId(), "计划回款", " ", " ",
					planingBack.getPlaningBackTime(), planingBack.getPlaningBackMoney(), "");
			saleclientDetailVos.add(clientDetailVo);
			jihuahuikuanMoney += planingBack.getPlaningBackMoney().doubleValue();
		}
		model.addAttribute("jihuahuikuanMoney", jihuahuikuanMoney);
		model.addAttribute("jihuahuikuanLength", planingBacks.size());

		Double huikuanMoney = 0.0;
		// 回款
		List<Back> backs = cODetailsService.getBackRecordListByClientId(cid);
		for (Back back : backs) {
			ClientDetailVo clientDetailVo = new ClientDetailVo(back.getBackId(), "回款", " ",
					clientInfoService.userName(clientInfo.getUserId().longValue()), back.getBackTime(),
					back.getBackMoney(), "已回");
			saleclientDetailVos.add(clientDetailVo);
			huikuanMoney += back.getBackMoney().doubleValue();
		}
		model.addAttribute("huikuanMoney", huikuanMoney);
		model.addAttribute("huikuanLength", backs.size());
		
		

		saleclientDetailVos.sort(new Comparator<ClientDetailVo>() {

			@Override
			public int compare(ClientDetailVo o1, ClientDetailVo o2) {
				if (o1.getDate() == null) {
					o1.setDate(java.sql.Date.valueOf("2018-09-03"));
				}
				if (o2.getDate() == null) {
					o2.setDate(java.sql.Date.valueOf("2018-09-03"));
				}
				int flag = o2.getDate().compareTo(o1.getDate());
				return flag;
			}
		});
		for (ClientDetailVo clientDetailVo : saleclientDetailVos) {
			if (clientDetailVo.getDate() == null) {
				clientDetailVo.setDate(java.sql.Date.valueOf("2018-09-03"));
			}
		}

		model.addAttribute("clientDetailVos", saleclientDetailVos);
		model.addAttribute("allLength", saleclientDetailVos.size());
		
		List<ClientDetailVo> proClientDetailVo = new ArrayList<>();
		
		//采购单
		Double caigouDanMoney = 0.0;
		List<Purchase> purchases = purchaseService.getPurchasesByClientId(cid);
		for (Purchase purchase : purchases) {
			caigouDanMoney += purchase.getPurmoney();
			ClientDetailVo clientDetailVo = new ClientDetailVo(
					purchase.getPurid(), 
					"采购单", 
					purchase.getPurtheme(), 
					clientInfoService.userName(purchase.getPurUserId()), 
					purchase.getPurdate(), 
					new BigDecimal(purchase.getPurmoney()), 
					purchase.getPurstatus());
			proClientDetailVo.add(clientDetailVo);
		}
		model.addAttribute("caigouDanMoney", caigouDanMoney);
		model.addAttribute("cangoudanLength", purchases.size());
		
		int cangoumingxiLength = 0;
		for (Purchase purchase : purchases) {
			List<Purchaseproductiondetail> purchaseproductiondetails = purproductDetailsService.findAllppdByPurid(purchase.getPurid());
			if (purchaseproductiondetails!=null) {
				for (Purchaseproductiondetail purchaseproductiondetail : purchaseproductiondetails) {
					ClientDetailVo clientDetailVo = new ClientDetailVo(
							purchaseproductiondetail.getPpdid(), 
							"采购明细", 
							purchaseproductiondetail.getPname(), 
							clientInfoService.userName(purchase.getPurUserId()), 
							purchase.getPurdate(), 
							new BigDecimal(purchaseproductiondetail.getPpmoney()), 
							purchase.getPurstatus());
					proClientDetailVo.add(clientDetailVo);
					cangoumingxiLength += 1;
				}
			}
		}
		model.addAttribute("cangoumingxiLength",cangoumingxiLength);		
		proClientDetailVo.sort(new Comparator<ClientDetailVo>() {

			@Override
			public int compare(ClientDetailVo o1, ClientDetailVo o2) {
				if (o1.getDate() == null) {
					o1.setDate(java.sql.Date.valueOf("2018-09-03"));
				}
				if (o2.getDate() == null) {
					o2.setDate(java.sql.Date.valueOf("2018-09-03"));
				}
				int flag = o2.getDate().compareTo(o1.getDate());
				return flag;
			}
		});
		model.addAttribute("proClientDetailVo", proClientDetailVo);
		model.addAttribute("proLength", proClientDetailVo.size());
		
		
		
		return "client/client-detail";
	}

	/**
	 * 客户视图中的联系人视图
	 * 
	 * @return
	 */
	@GetMapping("/to-client-view-contact")
	public String toClientContactView() {
		return "client/client-view-contact";
	}

	/**
	 * 客户视图中的编辑客户页面
	 * 
	 * @return
	 */
	@GetMapping("/to-client-detail-edit-clientInfo")
	public String toClientDetailEdit() {
		return "client/client-detail-edit-clientInfo";
	}

	/**
	 * 客户视图中根据客户编号查询客户
	 * 
	 * @param cid
	 * @return
	 */
	@PostMapping("client-detail-find-clientInfo-by-cid")
	@ResponseBody
	public ClientInfo findClientInfoByCid(Long cid) {
		return clientInfoService.load(cid);
	}

}
